import observe from "./observe";
import Dep from "./Dep";
export default function defineReactive(data, key, value) {
  const dep = new Dep();
  if (arguments.length == 2) {
    value = data[key]
  }
  // 子元素需要observe
  let childOb = observe(value);
  Object.defineProperty(data, key, {
    // 是否可以枚举
    enumerable: true,
    // 是否可配置的
    configurable: true,
    get() {
      console.log("你获取" + key);
      if (Dep.target) {
        dep.depend();
        if (childOb) {
          childOb.dep.depend();
        }
      }
      return value
    },
    set(newValue) {
      console.log("你设置" + key, newValue);
      if (value === newValue) {
        return;
      }
      value = newValue;
      childOb = observe(newValue);
      // 发布订阅模式，去通知
      dep.notify();
    }
  })
};